######################################################################
################## Section 3, sub-section 3.2 
################## Reliance on main bank 
################## Plot results 
######################################################################


# import libraries
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

path_data = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final datasets'
path_charts = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final charts\Credit concentration\Main bank share\order by large firms\final'
if not os.path.isdir(path_charts):
    os.makedirs(path_charts)


# define plotting function
def plot_mainBank(data, color, order):
    # data should be a dataframe
    # color should be a text
    # order should be a list of lists, where the first is order of all countries and the second is selected countries
        
    # single plots for selected countries
    sns.set(rc={'figure.figsize':(15,10), 'axes.labelsize':26, 'axes.titlesize':26,  'axes.labelpad':15, 'xtick.labelsize':26, 'ytick.labelsize':26}, style='white')
    for i, j in enumerate(['Large', 'Medium', 'Small', 'Micro']):
        plt.figure()
        sns.boxplot(data=data.loc[data['CNTRY'].isin(order[1])][data.loc[data['CNTRY'].isin(order[1])].sz_f == (i+1)], x='CNTRY', y='MB_SHR', width=[0.6], whis=[25, 75], showfliers=False, showmeans=True, medianprops={'color':'red', 'linewidth':4}, meanprops={'markerfacecolor':'red', 'markeredgecolor':'red', 'markersize':12}, color=color, order=order[1]).set(xlabel='', ylabel='Main bank share')
        plt.savefig(path_charts + '\\Section3_mbshr_bysz_' + j + '_201912.pdf', dpi=600, bbox_inches='tight')    

# load data
AC_final = pd.read_stata(path_data + '\dbtr_lvl_dt_f.dta')
AC_final.rename(columns={'cntry_dbtr':'CNTRY','nmbr_crdtrs':'NMBR_CRDTRS', 'mb_shr':'MB_SHR'}, inplace=True)
AC_final.drop(AC_final[AC_final.nace_code == ''].index, inplace=True)

# plot main bank share 
order_cntry_alph = ['AT','BE','CY','DE','EE','ES','FI','FR','GR','IE','IT','LT','LU','LV','MT','NL','PT','SI','SK']
sel_countries_alph = ['AT','BE','DE','ES','FI','FR','GR','IE','IT','NL','PT']
order_cntry = AC_final[AC_final.sz_f == 1].groupby(['CNTRY'])['MB_SHR'].mean().sort_values(ascending=False).index
order_slctd_cntry = order_cntry[order_cntry.isin(sel_countries_alph)]

plot_mainBank(data=AC_final, color='#7ccaa5', order=[order_cntry, order_slctd_cntry])
